package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ChangePwdBean;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.dialog.MyDialog;
import de.greenrobot.event.EventBus;

public class ChangePwdActivity extends BaseActivity implements View.OnClickListener {



    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.change_pwd)
    EditText changePwd;
    @Bind(R.id.change_new_pwd)
    EditText changeNewPwd;
    @Bind(R.id.change_confirm_pwd)
    EditText changeConfirmPwd;
    @Bind(R.id.change_submit)
    Button changeSubmit;

    MyDialog dialog;
    UserPresenter presenter;
    boolean isChangePwdClickable = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pwd);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        presenter = new UserPresenterImp();
        dialog = new MyDialog(this);
        dialog.setOnClickListener(new MyDialog.OnClickListener() {
            @Override
            public void click() {
                dialog.dismiss();
                finish();
            }
        });
        initClickbar();
        initToolbar();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    private void initClickbar() {
        changeSubmit.setOnClickListener(this);
    }

    private void initToolbar() {
        toolbar.setTitle("修改密码");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_submit: {
                if (isChangePwdClickable) {
                    showLoading();
                    isChangePwdClickable = false;
                    initChangePwd();
                }
                break;
            }
        }
    }

    private void initChangePwd() {
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        String pwd = changePwd.getText().toString();
        String newPwd = changeNewPwd.getText().toString();
        String confirmPwd = changeConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(pwd)) {
            DeviceUtil.MyToast(this, "请输入密码...");
            hideLoading();
            isChangePwdClickable = true;
            return;
        }
        if (!newPwd.equals(confirmPwd)) {
            DeviceUtil.MyToast(this, "两次输入的密码不一致哦...");
            hideLoading();
            isChangePwdClickable = true;
            return;
        }
        presenter.changePwd(dataEntity.getUserid(), pwd, newPwd, confirmPwd);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(ChangePwdBean bean) {

        isChangePwdClickable = true;
        hideLoading();
        if (bean.getReturn_code() == 0) {
            dialog.show();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
        }

    }

    public void onEventMainThread(ErrorBean bean) {
        hideLoading();
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }
}
